<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('notice_class',LIB_PATH);

$main = new Notice($db);
$id = intval($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller, $cf_action, 0);
        $sindex = isset($_GET['sindex']) ? $_GET['sindex'] : $_POST['sindex'];
        $stype = isset($_POST['stype']) ? $_POST['stype'] : $_GET['stype'];
        $self = isset($_POST['self']) ? intval($_POST['self']) : intval($_GET['self']);
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));
        $skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));
        $skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode(trim($_GET['skey3']));
        $where = "1";


        if ($skey) {
            $where .= " AND notice_title like '%" . $skey . "%'";
        }
		if ($stype && $stype !='2') {
            $where .= " AND notice_type ='" . $stype . "'";
        }



        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		$field="*";
        $data_ary = $MyPage->from_sql($field, 'data_notice', $where, "notice_id DESC", intval($_GET['cpage']), "20");
		foreach($data_ary as $k=>$v){
			  $file = explode('.',$v['notice_file']);
			  $data_ary[$k]['thumb'] = $file[1];
		  }
        $page = $MyPage->get_page($data, $cf_self_page . '?dpt='.$cf_dpt.'&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&stype=' . $stype . '&self=' . $self);

		
	break;
	
	case "add":
		if($cf_ispost){
		
		if($_POST['notice_dpt_all'] == '0' || $_POST['notice_type'] == '0'){
			$notice_dpt = '0';
		}else{
			$notice_dpt = implode(',',$_POST['notice_dpt']);
		}
		//my_print($notice_dpt);
			$data = array(	
				'notice_title'=>trim($_POST['notice_title']),
				'notice_dpt'=>$notice_dpt,
				'notice_dpt_type'=>intval($_POST['notice_dpt_type']) ? intval($_POST['notice_dpt_type']) : '0',
				'notice_type'=>intval($_POST['notice_type']),
				'notice_url'=>trim($_POST['notice_url']),
				'notice_file'=>trim($_POST['notice_file']),
				'notice_content'=>trim($_POST['notice_content']),
				'notice_creator'=>$cf_admin_info['admin_name'],
				'notice_create_time'=>date("Y-m-d H:i:s"),

				//'notice_type'=>$notice_type,											//公告类型
			);
			//$db->setDebug(1,1);
			$result = $main->insert_data($data);
			if($result){
				$more = $_SERVER['HTTP_REFERER'] ? '<span><a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a></span>' : '';
				my_jump('添加成功'.$more,'',$cf_self_page,3);
			}else{
			my_jump('添加失败');
			}
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;

		}

    break;
	
	case "edit":
		if($cf_ispost){
			$oldphoto = $_POST['oldphoto'];
			if($_POST['notice_dpt_all'] == '0' || $_POST['notice_type'] == '0'){
				$notice_dpt = '0';
			}else{
				$notice_dpt = implode(',',$_POST['notice_dpt']);
			}
			$data = array(
				'notice_title'=>trim($_POST['notice_title']),
				'notice_dpt'=>$notice_dpt,
				'notice_dpt_type'=>intval($_POST['notice_dpt_type']) ? intval($_POST['notice_dpt_type']) : '0',
				'notice_type'=>intval($_POST['notice_type']),
				'notice_url'=>trim($_POST['notice_url']),
				'notice_file'=>trim($_POST['notice_file']),
				'notice_content'=>trim($_POST['notice_content']),
				'notice_edit_info'=>$cf_admin_info['admin_name'].'  '.date("y-m-d H:i:s"),
			);

			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
			$result = $main->update_data($data,"notice_id='".$id."'");
			if($result){
				my_jump('修改成功','',$return_page,1);
            }else{
				my_jump('修改失败');
            }
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$row = $main->get_data_row($id);
			if(!$row){
				my_jump('This information does not exist or has been deleted');
			}
			if($row['notice_dpt']){
				$row['notice_dpt'] = explode(",",$row['notice_dpt']);
			}
			$_SESSION['pre_url']  = $cf_pre_page ? $cf_pre_page  : ''; 
		}
    break;
	
	case "del":
		$row = $main->get_data_row($id);
		if(!$row){
			my_jump('此信息已被删除或不存在');
		}
		//$main->del_photo($row['notice_file']);
		del_file($row['notice_file']);
		$result = $main->del_data("notice_id='$id'");
		if($result)
			my_jump('删除成功','',$cf_self_page,1);
		else
			my_jump('删除失败');
	break;
	case "delphoto":
		$row = $main->get_data_row($id);
		if(!$row){
			my_jump('此信息已被删除或不存在');
		}
		del_file(ROOT_PATH.trim($row['notice_file'],"/"));
		$result = $main->update_data(array('notice_file'=>''),"notice_id='".$id."'");
		if($result)
			my_jump('更新完成','',$cf_self_page,1);
		else
			my_jump('更新失败');
	break;
	case "detail":
		$smarty->mypage("notice_info",$cf_action,0) ;
			$row = $main->get_data_row($id);
			if(!$row){
				my_jump('此信息已被删除或不存在');
			}
	break;
	
	case "noticelist":
		$smarty->mypage("notice_info",$cf_action,0) ;
		load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
	

		if($cf_admin_info['g_root'] == '0'){
				$where = "notice_type='$id'";		
		}else{
			//$where = "notice_type='$id' and (notice_dpt like '%".$cf_admin_info['g_root']."%' OR notice_dpt='0')";
			if($id=="0"){
				$where ="notice_type='0'";
			}else{
				$where = "LOCATE(CONCAT(',',notice_dpt,','),',".$cf_admin_info['g_root'].",') > '0' AND notice_type='$id' OR LOCATE(CONCAT(',',notice_dpt,','),',300,') > '0' AND notice_type='$id' OR LOCATE(CONCAT(',',notice_dpt,','),',1100,') > '0' AND notice_type='$id'";
			}
		}
		$field="*";
		//$db->setDebug(1,1);
        $data_ary = $MyPage->from_sql($field, 'icc_notice', $where, "notice_create_time DESC", intval($_GET['cpage']), "20");
		foreach($data_ary as $k=>$v){
			  $file = explode('.',$v['notice_file']);
			  $data_ary[$k]['thumb'] = $file[1];
		  }
		$page = $MyPage->get_page($data, $cf_self_page . '?dpt='.$cf_dpt.'&self=' . $self);
	break;
	case "set":
		$type = $_GET['type'];
		$value = $_GET['value'];
		$where = "notice_id = '".$id."'";
		$result = $main->set_data($value,$type,$where);
		if($result)
			my_jump('更新完成','',$return_page,1);
		else 
			my_jump('更新失败');
	break;
	case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "alldel":
					$result = $main->del_data("notice_id in (".$list.")");
				break;
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('更新完成','',$cf_self_page,1);
			else
				my_jump('更新失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	case "download":
		$row = $main->get_data_row($id);
		load_file('download_class',LIB_PATH);
		$download=new download('php,html,htm,js,css',false);
		if(!$download->downloadfile(ROOT_PATH.trim($row['notice_file'],"/"))){
			$msg = $download->geterrormsg();
			echo "<script>alert('$msg');</script>";
			echo "<script>window.close();</script>";
			exit;
		}
		exit;
	break;
	default:
		my_jump('非法操作!');
	break;
}

$smarty->assign(array(
	'id'=>$id,
	'rid'=>$rid,
	'row'=>htmlchar($row),
	'data_ary'=>$data_ary,
	'sort_ary'=>$sort_ary,
	'page'=>$page,
	'skey' => $skey,
	'stype' => $stype,
	'cf_action' => $cf_action,

));



$smarty->mydisplay();

?>